from pynput import keyboard
from pynput import mouse
import time
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import *
from PyQt5.QtCore import *


# dell laptop
# ctrl_r ->page down, alt_r ->page up,f11->home,f12->end,home->f11,end->f12
#caps lock->disable(left click),insert->f1,page up->insert,page down->disable(right click)
# f1->disable(press)

#config
change_key_func_on = True
show_win = True
#config end

fix_pos = (0, 0)
cur_pos = (0, 0)
show_pos = False
fix_key = 'Key.pause'
move_pos = False

press_key2 = 'Key.scroll_lock'
def on_move(x,y):
    global cur_pos
    cur_pos = (x, y)
    
def on_click(x, y, button, pressed):
    global show_pos
    show_pos = False
    
mouse_listener = mouse.Listener(on_move=on_move,on_click=on_click)
mouse_listener.start()

key_pressed_when_shift = False
is_pressed = False

left_key = 'caps_lock'
left_key_replace = 'fsdfds'
left_key_replace_pressed = False

right_key = 'page_down'
right_key_replace = 'ctrl_r'
right_key_replace_pressed = False

press_key = 'Key.f1'
press_key_replace = 'insert'
press_key_replace_pressed = False

is_left_key_pressed = False

ctrl_pressed = False
alt_pressed = False
shift_pressed = False
num0_pressed = False

import json
ss = '''
[["<96>", 96], ["<97>", 97], ["<98>", 98], ["<99>", 99], ["<100>", 100], ["<101>", 101], ["<102>", 102], ["<103>", 103], ["<104>", 104], ["<105>", 105], ["<110>", 110], ["'+'", 107], ["'-'", 109], ["'*'", 106], ["'/'", 111]]
'''
print(ss)

numL = json.loads(ss)
print(numL)

def decode(key):
    if not hasattr(key, 'vk') :
        return -1
    for i in range(len(numL)):
        if numL[i] == [str(key), key.vk]:
            return i
    return -1
        
sum = 0
m = {1: [-1, 1],
     2: [0, 1],
     3: [1, 1],
     4: [-1, 0],
     6: [1, 0],
     7: [-1, -1],
     8: [0, -1],
     9: [1, -1],
     }

def on_press2(key):
    a = decode(key)
    #print(a)
    global sum
    if a == -1:
        sum = 0
        return
    if num0_pressed and a != 0:
        if a in m:
            dx = m[a][0] 
            dy =  m[a][1] 
            mouse.Controller().move(dx, dy)               
        sum = 0
        return        
    if (ctrl_pressed or shift_pressed or alt_pressed)  :
        sum = 0
        di = 0
        if ctrl_pressed and alt_pressed:
            di = 1
        elif shift_pressed:
            di = 1
        elif ctrl_pressed:
            di = 10
        elif alt_pressed:
            di = 100

        if a in m:
            dx = m[a][0] * di
            dy =  m[a][1] * di
            mouse.Controller().move(dx, dy)                    
        return
    if a >= 0 and a <= 9:
        sum = sum * 10 + a
        if sum > 10000:
            sum = 10000
        return
    if a == 10:
        mouse.Controller().click(mouse.Button.left)
        sum = 0
        return
    #  11+  12-  13* 14/
    if a == 14:
        mouse.Controller().move(0, sum)
        sum = 0
        return
    
    if a == 13:
        mouse.Controller().move(sum, 0)
        sum = 0
        return
    
    if a == 11:
        mouse.Controller().move(0, -sum)
        sum = 0
        return
    if a == 12:
        mouse.Controller().move(-sum, 0)
        sum = 0
        return

fix_key_time = time.time()

def on_press(key):
    vk = None
    if  hasattr(key, 'vk') :
        vk = key.vk    
    print('press', str(key), vk)
    
    global key_pressed_when_shift, is_pressed, is_left_key_pressed
    global left_key_replace_pressed, right_key_replace_pressed, press_key_replace_pressed
    global ctrl_pressed, shift_pressed, alt_pressed, num0_pressed, fix_pos, show_pos, fix_key_time, move_pos
    if fix_key == str(key):
        fix_pos = cur_pos
        show_pos = True
        if time.time() - fix_key_time < 0.3:
            move_pos = True
        fix_key_time = time.time()
        
    if press_key2 == str(key):    
        if not is_pressed:
            mouse.Controller().press(mouse.Button.left)
            print('p')
            is_pressed = True
        else:
            mouse.Controller().release(mouse.Button.left)
            is_pressed = False
        
    if 'ctrl' in str(key):
            ctrl_pressed = True
        
    if 'shift' in str(key):
        shift_pressed = True
        
    if 'alt' in str(key):
            alt_pressed = True
    a = decode(key)
    if a == 0:
        num0_pressed = True
    on_press2(key)
    if not change_key_func_on:
        return
    
    if left_key_replace in  str(key):
        left_key_replace_pressed = True
    if right_key_replace in  str(key):
        right_key_replace_pressed = True
    if press_key_replace in  str(key):
        press_key_replace_pressed = True                
        
    if left_key  in str(key) and not left_key_replace_pressed and not is_left_key_pressed:
        is_left_key_pressed = True
        mouse.Controller().press(mouse.Button.left)                   
        return
    
    if right_key  in str(key) and not right_key_replace_pressed:
        mouse.Controller().click(mouse.Button.right,1)                        
        return
        
    if press_key == str(key) and not press_key_replace_pressed:    
        if not is_pressed:
            mouse.Controller().press(mouse.Button.left)
            print('p')
            is_pressed = True
        else:
            mouse.Controller().release(mouse.Button.left)
            is_pressed = False
            
        
def on_release(key):
    vk = None
    if  hasattr(key, 'vk') :
        vk = key.vk        
    print('release', key, vk)
    global left_key_replace_pressed, right_key_replace_pressed, press_key_replace_pressed    
    global is_pressed, is_left_key_pressed
    global ctrl_pressed, shift_pressed, alt_pressed, num0_pressed
    a = decode(key)
    if a == 0:
        num0_pressed = False    
    if 'ctrl' in str(key):
            ctrl_pressed = False
        
    if 'shift' in str(key):
        shift_pressed = False
        
    if 'alt' in str(key):
            alt_pressed = False
    if not change_key_func_on:
        return
            
    if left_key_replace in  str(key):
        left_key_replace_pressed = False
    if right_key_replace in  str(key):
        right_key_replace_pressed = False
    if press_key_replace in  str(key):
        press_key_replace_pressed = False
    if left_key  in str(key) and not left_key_replace_pressed:
        is_left_key_pressed = False
        mouse.Controller().release(mouse.Button.left)                   
        return
        
    if 'ctrl' in str(key):    
        if is_pressed and not key_pressed_when_shift:
            print('hit')
            mouse.Controller().release(mouse.Button.left)
            is_pressed = False            
   
listener = keyboard.Listener(
    on_release=on_release,
    on_press=on_press,
    
)
listener.start()
import time
class my3(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.WindowStaysOnTopHint|Qt.FramelessWindowHint|Qt.ToolTip)
        #self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.setGeometry(100, 100, 50, 10)
        self.show()
        t = QTimer(self)
        t.timeout.connect(self.ont)
        t.start(20)
        h = QHBoxLayout(self)
        self.l = l = QLabel('aa')
        h.addWidget(l)
        self.setLayout(h)
        self.su = 0
        self.fix_pos = (0, 0)
        self.cur_pos = (0, 0)
        self.last_str = ''
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.hide()
        else:
            import sys
            sys.exit(0)
            self.close()
            print ("clicked", event)
    def ont(self):
        global move_pos
        if move_pos:
            move_pos = False
            self.setGeometry(cur_pos[0], cur_pos[1], 50, 10)
            self.show()
            
        st = self.last_str
        if show_pos:
            dx =  cur_pos[0] -fix_pos[0]
            if dx >= 0:
                dx = '*' + str(dx)
            else:
                dx = str(dx)
            dy =  cur_pos[1] -fix_pos[1]
            if dy > 0:
                dy = '/' + str(dy)
            else:
                dy = '+' + str(-dy)
            
            #st = '(%s,%s) (%s,%s) ' % (fix_pos[0], fix_pos[1], cur_pos[0] -fix_pos[0], cur_pos[1] -fix_pos[1])
            st = '(%s,%s) ' % ( dx, dy)
        if self.su != sum or st != self.last_str:
            self.su = sum
            self.last_str = st
            print(time.time(), 'ti', sum)
            self.l.setText(st+str(sum))
            
        
a = QApplication([])
w = my3()
a.exec_()
